package com.bleeth.fx.helper;

import cn.hutool.core.util.StrUtil;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Labeled;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

/**
 * @author ：Bleeth
 * @date ：2021-11-07
 * @description：Css辅助工具类
 */
public class StyleHelper {


    public static StyleHelper hover() {
        return null;
    }

    public static Bg bg(Region region) {
        return new Bg(region);
    }

    public static Ft ft(Node node) {
        return new Ft(node);
    }


    public static Br br(Region region) {
        return new Br(region);
    }

    public static void font() {
        Button button = new Button();
        button.setTextFill(Paint.valueOf(""));  //字体颜色
        button.setFont(Font.font(Font.getDefault().getName(), FontWeight.findByWeight(500), 14)); //设置字体
        //背景
        button.setBackground(new Background(new BackgroundFill(Paint.valueOf(""), new CornerRadii(5), new Insets(10))));
        button.setBackground(new Background(new BackgroundImage(new Image(""), null, null, null, null)));

        //设置border
        button.setBorder(new Border(new BorderStroke(null, null, new CornerRadii(5), null)));

    }

    public static class Br {
        private Region region;

        private Paint paint;

        private BorderStrokeStyle borderStrokeStyle;

        private CornerRadii cornerRadii;

        private BorderWidths borderWidths;


        public Br(Region region) {
            this.region = region;
        }

        public Br style(String style) {
            if (StrUtil.equalsIgnoreCase(style, "DOTTED")) {
                borderStrokeStyle = BorderStrokeStyle.DOTTED;
            }
            if (StrUtil.equalsIgnoreCase(style, "DASHED")) {
                borderStrokeStyle = BorderStrokeStyle.DASHED;
            }
            if (StrUtil.equalsIgnoreCase(style, "SOLID")) {
                borderStrokeStyle = BorderStrokeStyle.SOLID;
            }
            if (borderStrokeStyle == null) {
                borderStrokeStyle = BorderStrokeStyle.SOLID;
            }
            return this;
        }

        public Br width(double width) {
            borderWidths = new BorderWidths(width);
            return this;
        }


        public Br radius(double radius) {
            cornerRadii = new CornerRadii(radius);
            return this;
        }

        public Br color(String color) {
            paint = Paint.valueOf(color);
            return this;
        }

        public void make() {
            if (cornerRadii != null) {
                region.setBorder(new Border(new BorderStroke(paint, borderStrokeStyle, cornerRadii, borderWidths)));
            }
        }

    }

    public static class Ft {
        private Node node;

        private Paint paint;
        private TextAlignment textAlign;

        private String fontFamily;
        private double fontWeight;
        private int fontSize;

        public Ft(Node node) {
            this.node = node;
        }

        public Ft fill(String color) {
            paint = Paint.valueOf(color);
            return this;
        }

        public Ft align(String align) {
            textAlign = TextAlignment.valueOf(align);
            return this;
        }

        public Ft family(String family) {
            fontFamily = family;
            return this;
        }

        public Ft weight(double weight) {
            fontWeight = weight;
            return this;
        }

        public Ft size(int size) {
            fontSize = size;
            return this;
        }

        public void make() {
            if (node instanceof Labeled) {
                Labeled labeled = (Labeled) node;
                if (paint != null) {
                    labeled.setTextFill(paint);
                }
                if (textAlign != null) {
                    labeled.setTextAlignment(textAlign);
                }
                if (fontSize != 0) {
                    labeled.setFont(Font.font(Font.getDefault().getName(), FontWeight.findByWeight(500), fontSize));
                }
            }
            if (node instanceof Text) {
                Text text = (Text) node;
                if (fontSize != 0) {
                    text.setFont(Font.font(Font.getDefault().getName(), FontWeight.findByWeight(500), fontSize));
                }
                if (paint != null) {
                    text.setFill(paint);
                }
            }
        }
    }

    public static class Bg {

        private Region region;

        private Paint paint;
        private CornerRadii cornerRadii;
        private Insets insets;


        private Image image;
        private BackgroundRepeat repeatX;
        private BackgroundRepeat repeatY;
        private BackgroundPosition position;

        public Bg(Region region) {
            this.region = region;
        }

        public Bg color(String color) {
            paint = Paint.valueOf(color);
            return this;
        }

        public Bg lineaGradient(String color) {
            paint = Paint.valueOf(color);
            return this;
        }

        public Bg radius(double radius) {
            cornerRadii = new CornerRadii(radius);
            return this;
        }

        public Bg insets(double insetsValue) {
            insets = new Insets(insetsValue);
            return this;
        }


        public Bg image(String url) {
            image = new Image(url);
            return this;
        }

        public Bg repeatX(String X) {
            repeatX = BackgroundRepeat.valueOf(X);
            return this;
        }

        public Bg repeatY(String y) {
            repeatY = BackgroundRepeat.valueOf(y);
            return this;
        }

        public Bg position(String positionValue) {
            if (StrUtil.equalsIgnoreCase(positionValue, "CENTER")) {
                position = BackgroundPosition.CENTER;
            } else {
                position = BackgroundPosition.DEFAULT;
            }
            return this;
        }


        public void make() {
            if (image != null) {
                region.setBackground(new Background(new BackgroundImage(image, repeatX, repeatY, position, null)));
            }
            if (paint != null || cornerRadii != null) {
                region.setBackground(new Background(new BackgroundFill(paint, cornerRadii, insets)));
            }
        }
    }

}
